﻿using System.Diagnostics;
using System.IO;

using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace Utilities.Tests
{
    [TestClass()]
    public class DirectoryHelperTest
    {
        private TestContext testContextInstance;

        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #region Additional test attributes
        // 
        //You can use the following additional attributes as you write your tests:
        //
        //Use ClassInitialize to run code before running the first test in the class
        //[ClassInitialize()]
        //public static void MyClassInitialize(TestContext testContext)
        //{
        //}
        //
        //Use ClassCleanup to run code after all tests in a class have run
        //[ClassCleanup()]
        //public static void MyClassCleanup()
        //{
        //}
        //
        //Use TestInitialize to run code before running each test
        //[TestInitialize()]
        //public void MyTestInitialize()
        //{
        //}
        //
        //Use TestCleanup to run code after each test has run
        //[TestCleanup()]
        //public void MyTestCleanup()
        //{
        //}
        //
        #endregion

        [TestMethod()]
        public void GetRelativePathRoot()
        {
            string directory = @"C:";
            string path = @"C:\Project\Class1.cs";

            Assert.AreEqual(@"Project\Class1.cs", DirectoryHelper.GetRelativePath(directory, path));
        }

        [TestMethod()]
        public void GetRelativePathParent()
        {
            string directory = @"C:\Project";
            string path = @"C:\";

            Assert.AreEqual(@"..\", DirectoryHelper.GetRelativePath(directory, path));
        }

        [TestMethod()]
        public void GetRelativePathDifferentPathsTest()
        {
            string directory = @"C:\Inetpub\wwwroot\Project1\Master\Dev\SubDir1";
            string path = @"C:\Inetpub\wwwroot\Project1\Master\Dev\SubDir2\SubDirIWant\Class1.cs";

            Assert.AreEqual(@"..\SubDir2\SubDirIWant\Class1.cs", DirectoryHelper.GetRelativePath(directory, path));
            Assert.AreEqual(@"..\..\..\SubDir1", DirectoryHelper.GetRelativePath(path, directory));
        }

        [TestMethod()]
        public void TraverseTest()
        {
            DirectoryHelper.Traverse(@"C:\inetpub",
                (directoryInfo, relativePath, level) =>
                {
                    Debug.Write(new string(' ', level * 2));
                    Debug.Write("Directory: '" + directoryInfo.Name + "' - RelativePath: ");
                    Debug.WriteLine(relativePath);

                    foreach (FileInfo fileInfo in directoryInfo.GetFiles())
                    {
                        Debug.Write(new string(' ', (level + 1) * 2));
                        Debug.WriteLine("File name: '" + fileInfo.Name + "'");
                    }
                },
                (exception, directoryInfo, relativePath, level) =>
                {
                    Debug.WriteLine(exception.Message);

                    return true;
                });

        }

        [TestMethod()]
        public void CopyDirectoryTest()
        {
            DirectoryHelper.CopyDirectory(@"C:\inetpub", @"D:\InetBackup",
                (absolutePath) =>
                {
                    Debug.WriteLine("File created: '" + absolutePath + "'");
                },
                (exception, directoryInfo, relativePath, level) =>
                {
                    Debug.WriteLine(exception.Message);

                    return true;
                });
        }
    }
}
